package fact.view.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UICommand;
import javax.faces.component.UIInput;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpSession;

import fact.view.DTO.FactDTO;
import fact.view.DTO.NodeFactDTO;
import fact.view.DTO.NodeHierarchyDTO;
import fact.view.DTO.ResponsibilityDTO;
import fact.view.DTO.ScenarioDTO;
import fact.view.model.FactModelBean;
import fact.view.util.MyApplicationContext;
import fact.view.util.ServletUtil;

@ManagedBean(name = "factBean")
@RequestScoped
//@ViewScoped
//@CustomScoped(value = "#{window}")
public class FactBean extends ServletUtil implements Serializable  {

	private static final long serialVersionUID = -7617917330249050697L;
	
	private List<FactDTO> factData;
	private List<ScenarioDTO> scenarioList;
	private List<NodeHierarchyDTO> nodeHierarchy;
	 
    @ManagedProperty(value="#{factModelBean}")
    private FactModelBean factModelBean;
    
    @ManagedProperty(value="#{loginBean}")
    private LoginBean loginBean;
    
    private String month;
    private String year;

    
    private int     segmentId;
    private String  segmentName;
    private int     makeId;
    private String  makeName;
    private int     marketId;
    private String  marketName;
	
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	
	 public void selectOneChanged(ValueChangeEvent event) {
		 FacesContext.getCurrentInstance().renderResponse();
		 FactBean myBean = (FactBean) FacesContext.getCurrentInstance().getELContext().getELResolver().getValue(FacesContext.getCurrentInstance().getELContext(), null, "factBean");
		 myBean.getFactData().clear();
		 
		 
		 try
			{
			
				this.factData=factModelBean.getFactList(loginBean.getLoginName(),month,year);
			}
			catch (Exception e) {
				
			}
	 }
	 

	
	 
	public String showSegmentForm_old() {
		
		//Map<String,String> params =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		//int action =Integer.valueOf(params.get("segmentId"));
		
		FacesContext fc = FacesContext.getCurrentInstance();
		//this.segmentId = getSegmentParam(fc);
		
		if(month==null && year==null)
		{
			Calendar cal = Calendar.getInstance();  
		    int year_ = cal.get(cal.YEAR);  
		    int month_ = cal.get(cal.MONTH);
			month=String.valueOf(month_);
			year=String.valueOf(year_);
		}

		try
		{
		
			//this.factData=factModelBean.getFactList(loginBean.getLoginName(),month,year);
			//System.out.println("Segment Id:"+segmentId);
			//this.factData=factModelBean.getBlankSegmentForm(loginBean.getLoginName(),segmentId,month,year);
			this.scenarioList=factModelBean.getAllScenario();
			
		}
		catch (Exception e) {
			
		}
		
		
		return "factEntry.jsf";
	}
	
	public String getSegmentForm() {
		
		FacesContext fc = FacesContext.getCurrentInstance();
		
		this.segmentId = Integer.parseInt(getRequestParam(fc,"segmentId"));
		this.makeId = Integer.parseInt(getRequestParam(fc,"makeId"));
		this.marketId = Integer.parseInt(getRequestParam(fc,"marketId"));
		
		this.segmentName = getRequestParam(fc,"segmentName");
		this.makeName = getRequestParam(fc,"makeName");
		this.marketName = getRequestParam(fc,"marketName");
//		month="1";
//		year="2012";
		if(month==null && year==null)
		{
			Calendar cal = Calendar.getInstance();  
		    int year_ = cal.get(cal.YEAR);  
		    int month_ = cal.get(cal.MONTH);
			month=String.valueOf(month_);
			year=String.valueOf(year_);
		}

		try
		{
		
			//this.factData=factModelBean.getFactList(loginBean.getLoginName(),month,year);
			System.out.println("Segment Id:"+segmentId);
			this.nodeHierarchy=factModelBean.getNodeHierarchy(segmentId, makeId, marketId,Integer.parseInt(month),Integer.parseInt(year));
			//(loginBean.getLoginName(),segmentId,month,year);
			this.scenarioList=factModelBean.getAllScenario();
			session.setAttribute("session_scenarioList", scenarioList);
			session.setAttribute("session_nodeHierarchy", nodeHierarchy);
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		//return null;
		return "factEntry.jsf";
	}	

	public String reportHome() {
		return "reportHome.jsf";
	}
	
	  public void submit(ActionEvent ae)
	    {
	    	String msg="";
	    	
	    	if(month.equalsIgnoreCase("99") || year.equalsIgnoreCase("9999"))
	    	{
	    		msg="Please select Month and Year Correctly";
	    	}
	    	
	    	if(!msg.equalsIgnoreCase(""))
	    	{
	    	FacesMessage facesMessage=new FacesMessage(msg);
	    	FacesContext facesContext=FacesContext.getCurrentInstance();
	    	String clientId=null;
	    	facesContext.addMessage(clientId,facesMessage);
	    	}
	    }
	  
	public void saveFacts() 
	{
		System.out.println("Here I'm ");
		boolean response=false;
		try{
		 response=factModelBean.saveFacts(factData,loginBean.getLoginName(),month,year);
		}
		
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	
	public void submitFactForm()
	{
		System.out.println("in submitFactForm() Method......");
		System.out.println(month);
		FactBean factBeanEty = (FactBean)MyApplicationContext.getManagedBean("factBean");
		System.out.println(factBeanEty.getNodeHierarchy().size());
		
		for(int i=0;i<nodeHierarchy.size();i++)
		{
			NodeHierarchyDTO node=nodeHierarchy.get(i);
			ArrayList<NodeFactDTO> nodeFact=node.getNodeFactList();
			
			for(int j=0;j<nodeFact.size();j++)
			{
				System.out.println("Entry Set Id :"+nodeFact.get(j).getEntrySetId());
				System.out.println("node Id :"+nodeFact.get(j).getNodeId());
				System.out.println("scenario Id :"+nodeFact.get(j).getScenarioId());
				System.out.println("Value :"+nodeFact.get(j).getValue());
			}
			
		}
	}


    public void nodeCheck(ActionEvent ae)
    {
    	 UICommand link = (UICommand) ae.getComponent();
    	    UIParameter param = (UIParameter) link.getChildren().get(0);
    	   int currentID =  Integer.parseInt(param.getValue().toString());
    	    
    	System.out.println("Segment Id :"+currentID);
    	String msg="notallowed";
    	FacesMessage facesMessage=new FacesMessage(msg);
    	FacesContext facesContext=FacesContext.getCurrentInstance();
    	String clientId=null;
    	facesContext.addMessage(clientId,facesMessage);
    	
    
    }
    
    public void actionListener(ActionEvent actionEvent) {
        // Add event code here...
        System.out.println("Made it!");
    }
    
    public void saveFactValue(AjaxBehaviorEvent event)
    {
//    	Integer entrySetId =(Integer) ((UIInput) vcEvent.getSource()).getAttributes().get("entrySetId");
//    	Integer nodeId =(Integer) ((UIInput) vcEvent.getSource()).getAttributes().get("nodeId");
//    	Integer scenarioId =(Integer) ((UIInput) vcEvent.getSource()).getAttributes().get("scenarioId");
//    	
//    	System.out.println(entrySetId);
//    	System.out.println(nodeId);
//    	System.out.println(scenarioId);
    	
    	System.out.println("Ifti");
    	
    	
    }
    
    public void codeChanged(ValueChangeEvent event) {
        UIInput menu = (UIInput) event.getComponent();
        String val = (String)((UIInput) event.getSource()).getAttributes().get("param1");
        System.out.println(val);
        
        scenarioList=(List<ScenarioDTO>)session.getAttribute("session_scenarioList");
        nodeHierarchy=(List<NodeHierarchyDTO>)session.getAttribute("session_nodeHierarchy");
        
        
    }
    public void reset1(ActionEvent ae) {
        System.out.println("abc");
    }
    
    public void persistanceAction(final ActionEvent event) {
    	System.out.println("In persisstanceAction");
    }
    public void saveFactValue1(ValueChangeEvent e){
		System.out.println("jami");
	}
    @PostConstruct
    public void init()
    {
        System.out.println("Done constructing testBean.");
    }
    
	public List<FactDTO> getFactData() {
		
		 return factData;
	}

	public void setFactData(List<FactDTO> factData) {
		this.factData = factData;
	}

	public FactModelBean getFactModelBean() {
		return factModelBean;
	}

	public void setFactModelBean(FactModelBean factModelBean) {
		this.factModelBean = factModelBean;
	}

	public LoginBean getLoginBean() {
		return loginBean;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}


	public List<ScenarioDTO> getScenarioList() {
		return scenarioList;
	}


	public void setScenarioList(List<ScenarioDTO> scenarioList) {
		this.scenarioList = scenarioList;
	}

	public int getSegmentId() {
		return segmentId;
	}

	public void setSegmentId(int segmentId) {
		this.segmentId = segmentId;
	}

	public String getSegmentName() {
		return segmentName;
	}

	public void setSegmentName(String segmentName) {
		this.segmentName = segmentName;
	}

	public int getMakeId() {
		return makeId;
	}

	public void setMakeId(int makeId) {
		this.makeId = makeId;
	}

	public String getMakeName() {
		return makeName;
	}

	public void setMakeName(String makeName) {
		this.makeName = makeName;
	}

	public int getMarketId() {
		return marketId;
	}

	public void setMarketId(int marketId) {
		this.marketId = marketId;
	}

	public String getMarketName() {
		return marketName;
	}

	public void setMarketName(String marketName) {
		this.marketName = marketName;
	}

	public List<NodeHierarchyDTO> getNodeHierarchy() {
		return nodeHierarchy;
	}

	public void setNodeHierarchy(List<NodeHierarchyDTO> nodeHierarchy) {
		this.nodeHierarchy = nodeHierarchy;
	}

	



}